﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/08/18
 * 
 * 功能说明：菜单配置
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
using Whir.Common;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Config_MenuConfig : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (!IsPostBack)
        {
            //绑定菜单到Tree
            BindTree(TreeView1, "站点菜单配置", "systemMenu.config");
            BindTree(TreeView2, "工具菜单配置", "toolsMenu.config");
            BindTree(TreeView3, "帮助菜单配置", "helpMenu.config");
        }
    }

    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        string fileName = "";
        fileName = Server.MapPath(Settings.GetString("MenuPath") + "systemMenu.config").ToString();
        Save(TreeView1, fileName);

        fileName = Server.MapPath(Settings.GetString("MenuPath") + "toolsMenu.config").ToString();
        Save(TreeView2, fileName);

        fileName = Server.MapPath(Settings.GetString("MenuPath") + "helpMenu.config").ToString();
        Save(TreeView3, fileName);

        this.OLAdd();
    }

    //操作日志
    protected void OLAdd()
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "菜单配置成功";
        model.operateType = 2;      //修改
        model.operateResult = true;
        model.operateDesc = "菜单配置成功";

        Whir.Log.OperateLog.Add(model);
    }

    /// <summary>
    /// 绑定 Tree型菜单
    /// </summary>
    /// <param name="tvMenuTree"> 设置TreeView 控件</param>
    /// <param name="parentText"> 设置第一级标题 </param>
    /// <param name="xmlFile"> 设置绑定的XML 文件名称</param>
    private void BindTree(TreeView tvMenuTree, string parentText, string xmlFile)
    {
        tvMenuTree.Nodes.Clear();
        TreeNode node = new TreeNode();
        node.Text = parentText;
        node.Value = "0";
        node.PopulateOnDemand = false;
        node.SelectAction = TreeNodeSelectAction.Expand;
        node.Expanded = true;
        tvMenuTree.Nodes.Add(node);

        BindNode(node, xmlFile);
    }

    private void BindNode(TreeNode node, string xmlFile)
    {
        try
        {
            XmlDocument menuXd = new XmlDocument();
            //加载XML
            menuXd.Load(Server.MapPath(Settings.GetString("MenuPath") + xmlFile).ToString());

            //获取根结点
            XmlNode xnFather = menuXd.DocumentElement;

            //获取一级菜单结点
            XmlNodeList firstLevelList = xnFather.ChildNodes;

            TreeNode nodeItem;
            TreeNode newNodeItem;

            for (int i = 0; i < firstLevelList.Count; i++)
            {
                //获取二级菜单结点
                XmlNode xn = firstLevelList[i];

                nodeItem = new TreeNode();
                nodeItem.Text = xn.Attributes["title"].Value;
                nodeItem.Value = xn.Attributes["title"].Value;
                nodeItem.SelectAction = TreeNodeSelectAction.Expand;
                nodeItem.Expanded = true;
                //nodeItem.Checked = Convert.ToBoolean(xn.Attributes["visible"].Value);

                for (int j = 0; j < xn.ChildNodes.Count; j++)
                {
                    //获取三级菜单结点
                    XmlNode child = xn.ChildNodes[j];
                    newNodeItem = new TreeNode();
                    newNodeItem.Text = child.Attributes["title"].Value;
                    newNodeItem.Value = child.Attributes["title"].Value;
                    newNodeItem.SelectAction = TreeNodeSelectAction.None;
                    newNodeItem.Expanded = true;
                    newNodeItem.Checked = Convert.ToBoolean(child.Attributes["visible"].Value);
                    nodeItem.ChildNodes.Add(newNodeItem);
                }

                node.ChildNodes.Add(nodeItem);
            }
        }
        catch (XmlException ex)
        {
            throw new Exception(ex.Message);
        }
    }

    //保存XML配置
    private void Save(TreeView treeview, string fileName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(fileName);

        //先全部置为false
        XmlNodeList nodelist0 = xmlDoc.SelectSingleNode("menu").ChildNodes;

        foreach (XmlNode xn in nodelist0)
        {
            XmlNodeList childList = xn.ChildNodes;
            foreach (XmlNode xnChild in childList)
            {
                XmlElement xeChild = (XmlElement)xnChild;

                xeChild.SetAttribute("visible", "false");
            }
        }
        //end

        foreach (TreeNode cb in treeview.CheckedNodes)
        {
            string nodeText = cb.Text;

            XmlNodeList nodelist = xmlDoc.SelectSingleNode("menu").ChildNodes;

            foreach (XmlNode xn in nodelist)
            {
                XmlNodeList childList = xn.ChildNodes;
                foreach (XmlNode xnChild in childList)
                {
                    XmlElement xeChild = (XmlElement)xnChild;
                    if (xeChild.GetAttribute("title") == nodeText)
                    {
                        xeChild.SetAttribute("visible", "true");
                        break;
                    }
                }
            }
        }

        xmlDoc.Save(fileName);
    }

}

